package com.ruoyi.syrw.aspectj;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.syrw.annotation.FIllBasicInfoForTaiZhang;
import com.ruoyi.report_tg.domain.vo.ExcelGeneralVo;
import com.ruoyi.syrw.domain.RefReportExcel;
import com.ruoyi.syrw.domain.vo.BsDataReportVo;
import com.ruoyi.syrw.mapper.BiExcelTypeMapper;
import com.ruoyi.syrw.mapper.BsDataReportMapper;
import com.ruoyi.syrw.mapper.RefReportExcelMapper;
import com.ruoyi.syrw.service.IBsDataReportService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.ycl.domain.BsJctz;
import com.ruoyi.ycl.domain.vo.BsJctzVo;
import com.ruoyi.ycl.mapper.BsJctzMapper;
import com.ruoyi.yp.domain.BsYp;
import com.ruoyi.yp.domain.vo.BsYpVo;
import com.ruoyi.yp.mapper.BsYpMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * 填充 基础的信息
 *
 * @author zyb
 */
@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
@Order(100)
public class FIllInfoForTaiZhang {

    private final RefReportExcelMapper refReportExcelMapper;
    private final BsDataReportMapper reportMapper;
    private final IBsDataReportService reportService;
    private final BsYpMapper ypMapper;
    private final ISysDictDataService dictDataService;
    private final ISysDeptService deptService;
    private final BsJctzMapper jctzMapper;
    private final BiExcelTypeMapper excelTypeMapper;

    /**
     * 填充字段
     * 处理完请求后执行
     * 与普通版本区别： 不考虑空表打印情况，不考虑分页
     *
     * @param joinPoint 切点
     */
    @Around(value = "@annotation(fIllBasicInfoForTaiZhang)")
    public Object fillResult(ProceedingJoinPoint joinPoint, FIllBasicInfoForTaiZhang fIllBasicInfoForTaiZhang) throws Throwable {
        // 2024年7月19日 10:16:10 add excelId为空时的处理
//        Long excelId = (Long) joinPoint.getArgs()[0];
        ExcelGeneralVo result = (ExcelGeneralVo) joinPoint.proceed();

//        // 试验室信息 （只能拿到用户自己所在试验室的信息）
//        SysDept sysDept = deptService.selectDeptById(getDeptId());
//        result.setJiancdwmc(sysDept.getTestingCompany());
//        result.setGongcmc(sysDept.getConstructionProject());
//        result.setShigwtdw(sysDept.getConstructionCompany());

        Long reportId = refReportExcelMapper.selectVoOne(new LambdaQueryWrapper<RefReportExcel>()
            .eq(RefReportExcel::getExcelId, result.getExcelId())).getReportId();
        BsDataReportVo reportVo = reportMapper.selectVoById(reportId);
        // 样品信息
        BsYpVo bsYpVo = ypMapper.selectVoOne(new LambdaQueryWrapper<BsYp>().eq(BsYp::getYpNum, reportVo.getYpCode()));
        result.setYangpmc(bsYpVo.getYangpmc());
        result.setYangpsl(bsYpVo.getYangpsl());
        result.setYangpzt(bsYpVo.getYangpzt());
        result.setYangpsj(ObjUtil.isNotNull(bsYpVo.getYpDate()) ? DateUtil.format(bsYpVo.getYpDate(), Constants.EXCEL_DATE_FORMAL) : null);
        result.setGongcbwyt(bsYpVo.getGcbw());
        result.setDaibsl(bsYpVo.getDaibsl());
        result.setQuyr(bsYpVo.getYpPerson());
        // 样品编号
        result.setYangpbh(reportVo.getYpCode());
        // 是否合格
        result.setReportResult(2 == reportVo.getReportResult() ? "不合格" : "合格");

        // 进场台账信息
        if (ObjUtil.isNotNull(reportVo.getTzId())) {
            BsJctzVo bsJctzVo = jctzMapper.selectVoOne(new LambdaQueryWrapper<BsJctz>().eq(BsJctz::getTzId, reportVo.getTzId()));
            // 取样地点 / 取样位置
            result.setQuydd(bsJctzVo.getCunfwz());
            // 产地 / 生产厂家
            result.setCd(bsJctzVo.getChandcj());
            // 规格型号 / 样品规格
            result.setGuigxh(bsJctzVo.getGuigxh());
            // 批号
            result.setPih(bsJctzVo.getPih());
            // 进场日期
            result.setJincrq(ObjUtil.isNotNull(bsJctzVo.getInDate()) ? DateUtil.format(bsJctzVo.getInDate(), Constants.EXCEL_DATE_FORMAL) : null);
            // 出厂日期
            result.setChucrq(ObjUtil.isNotNull(bsJctzVo.getChucDate()) ? DateUtil.format(bsJctzVo.getChucDate(), Constants.EXCEL_DATE_FORMAL) : null);

        }
        // mod 2024-8-26 试验检测日期增加第二个字段 begin
        if (ObjUtil.isNull(reportVo.getReportDateEnd())) {
            result.setShiyjcrq(ObjUtil.isNotNull(reportVo.getReportDate()) ? DateUtil.format(reportVo.getReportDate(), Constants.EXCEL_DATE_FORMAL) : null);
        }else {
            result.setShiyjcrq(DateUtil.format(reportVo.getReportDate(), Constants.EXCEL_DATE_FORMAL) + "～"
                + DateUtil.format(reportVo.getReportDateEnd(), Constants.EXCEL_DATE_FORMAL));
        }
        // mod 2024-8-26 试验检测日期增加第二个字段 end

        // 考虑到 以下信息 既要引用,又要可编辑,只能单向关联. -- 202040816 改为不能编辑 强制填充引用
        // 记录编号
//        result.setJilbh(reportVo.getJlCode());
        // 报告编号
        result.setBaogbh(reportVo.getBgCode());


        return result;
    }

}
